var app = getApp()
var util = require('../../utils/util.js')

Page({
    data: {
        inTheaters: {},
        comingSoon: {},
        top250: {},
        searchResult: {},
        containerShow: true,
        searchPanelShow: false
    },

    onLoad(options) {
        let that = this
        var inTheatersUrl = app.globalData.doubanBase + 'v2/movie/in_theaters' + '?start=0&count=3'
        var comingSoonUrl = app.globalData.doubanBase + 'v2/movie/coming_soon' + '?start=0&count=3'
        var top250 = app.globalData.doubanBase + 'v2/movie/top250' + '?start=0&count=3'

        that.getMovieListData(inTheatersUrl, 'inTheaters', '正在热映')
        that.getMovieListData(comingSoonUrl, 'comingSoon', '即将上映')
        that.getMovieListData(top250, 'top250', '豆瓣Top250')
    },

    // 跳转到电影详情
    onMovieTap(event) {
        var movieId = event.currentTarget.dataset.movieid
        wx.navigateTo({
            url: 'movie-detail/movie-detail?id=' + movieId
        })
    },

    getMovieListData(url, settedKey, catetoryTitle) {
        let that = this
        wx.request({
            url: url,
            method: 'GET',
            header: {
                'Content-Type': 'application/xml'
            },
            success: function (res) {
                // console.log(res.data)
                that.processDoubanData(res.data, settedKey, catetoryTitle)
            },
            fail: function (err) {
                console.log(err)
            }
        })
    },

    // 点击关闭搜索
    onCancelImgTap() {
        this.setData({
            containerShow: true,
            searchPanelShow: false
            // searchResult: {} // 搜索内容置空
        })
    },

    // 输入框聚焦时触发
    onBindFocus() {
        this.setData({
            containerShow: false,
            searchPanelShow: true
        })
    },

    onBindBlur(event) {
        var text = event.detail.value
        var searchUrl = app.globalData.doubanBase + 'v2/movie/search?q=' + text
        this.getMovieListData(searchUrl, 'searchResult', '')
    },

    processDoubanData(moviesDouban, settedKey, catetoryTitle) {
        var that = this
        var movies = []
        for (var idx in moviesDouban.subjects) {
            var subject = moviesDouban.subjects[idx]
            var title = subject.title

            if (title.length >= 6) {
                title = title.substring(0, 6) + '...'
            }
            var temp = {
                stars: util.convertToStarsArray(subject.rating.stars),
                title: title,
                average: subject.rating.average,
                coverageUrl: subject.images.large,
                movieId: subject.id
            }
            movies.push(temp)
        }
        var readyData = {}
        readyData[settedKey] = {
            catetoryTitle: catetoryTitle,
            movies: movies
        }
        this.setData(readyData)
    },

    // 更多跳转
    onMoreTap(event) {
        var category = event.currentTarget.dataset.category
        wx.navigateTo({
            url: 'more-movie/more-movie?category=' + category
        })
    }
})